<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class gojp_json extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
		// Call the Model constructor
		parent::_construct();
	}
	function getexcurslist($client,$agent)
	{
		$query='select idUser from podpisannie where idUser='.$client.' and idPodp='.$agent;
		$result=$this->db->query($query);
		$responce=array();
		$excurs=array();
		$city=array();
		$idc=array();
		if($result->num_rows>0||$agent==$client)
		{
			$query="select s.id,s.name,l.`desc`,l.idoption from service s
			left join listoptions l on l.idservice = s.id and idoption in (7,4,29)
			where iduser=".$client." and idtype=2 and s.show=1";
			$result=$this->db->query($query);
			foreach($result->result() as $row)
			{
				$excurs[$row->id]['name']=$row->name;
				$excurs[$row->id]['id']=$row->id;
				
				switch($row->idoption)
				{
					case '7':
						$excurs[$row->id]['walk']=$row->desc;
					break;
					case '4':
						$excurs[$row->id]['prod']=$row->desc;
					break;
					case '29':
						$city[$row->desc][]=$row->id;
						$idc[]=$row->desc;
					break;
				}
			}
			if(count($idc)>0)
			{
				$query="SELECT id,name FROM service where id in (".implode(',',$idc).")";
				$result=$this->db->query($query);
				foreach($result->result() as $row)
				{
					foreach($city[$row->id] as $eid)
					{
						$excurs[$eid]['city']=$row->name;
					}
				}
			}
		}
		return json_encode($excurs);
	}
	function getshowplacelist($pto,$agent)
	{
		
		$query='select idUser from podpisannie where idUser='.$pto.' and idPodp='.$agent;
		$result=$this->db->query($query);
		$responce=array();
		$showplace=array();
		if($result->num_rows>0||$agent==$pto)
		{
			$query="select s.id,s.name,s.`desc`,l.desc as coord from service s
					left join listoptions l on l.idservice = s.id and idoption=60
					where iduser=".$pto." and idtype=4 and s.show=1";
			$result=$this->db->query($query);
			foreach($result->result() as $row)
			{
				$showplace[$row->id]['name']=$row->name;
				$showplace[$row->id]['description']=$row->desc;
				$showplace[$row->id]['coordinates']=$row->coord;
				$showplace[$row->id]['excurs']=array();
			}
			$query="select s.id,s.name,l.desc as idshowplace from service s
					left join listoptions l on l.idservice = s.id and idoption=30
					where iduser=".$pto." and idtype=2 and s.show=1";
			$result=$this->db->query($query);
			foreach($result->result() as $row)
			{
				$showplace[$row->idshowplace]['excurs'][$row->id]=$row->name;
			}
		}
		return json_encode($showplace);
	}
	function full($id)
	{
		$query="select `desc` from service where id=".$id;
		$result=$this->db->query($query)->result_array();
		$responce=array();
		$responce['description']=$result[0]['desc'];
		$query="select s.id,s.name,s.`desc` from service s
			inner join listoptions l on s.id=l.desc and l.idoption=30 and l.idservice=".$id;
		$result=$this->db->query($query);
		$responce['showplace']=array();
		foreach($result->result() as $row)
		{
			$responce['showplace'][$row->id]['id']=$row->id;
			$responce['showplace'][$row->id]['name']=$row->name;
			$responce['showplace'][$row->id]['description']=$row->desc;
			$responce['foto']=array();
		}
		$query="select f.url from fotos  f
			inner join  listfotos lf on lf .idservice=".$id." and f.id=lf.idfoto";
		$result=$this->db->query($query);
		foreach($result->result() as $row)
		{
			$responce['foto'][]=$row->url;
		}
		return json_encode($responce);
	}
	function price($id,$date,$type)
	{
		if($type==1)
		{
			$type="Груп";
			$not='';
		}
		else
		{
			$type="Груп";
			$not='not';
		}
		$dateA=explode('-',$date);
		$date2  = date('Y-m-d',mktime(0, 0, 0, $dateA[1]  , $dateA[2]+9, $dateA[0]));
		$this->log($date2);
		$query="SELECT p.idrow, p.date, p.price, s.id,l.name,l.desc
				FROM price p
				INNER JOIN listoftables l ON p.idtable = l.id
				AND l.name $not LIKE  '$type%'
				INNER JOIN service s ON s.iduser = $id
				AND s.id = l.idservice
				AND s.idtype = 2
				WHERE  `date` between STR_TO_DATE('$date', '%Y-%m-%d') AND STR_TO_DATE('$date2', '%Y-%m-%d')";
		$result=$this->db->query($query);
		$responce=array();
		$i=0;
		foreach($result->result() as $row)
		{
			$responce[$row->id]['dates'][]=$row->date;
			$responce[$row->id]['price'][$row->name][$row->idrow]=$row->price;
			$responce[$row->id]['price'][$row->name]['desc']=$row->desc;

			$i++;
		}
		return json_encode($responce);
	}
}